package com.chris.easymallproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chris.common.utils.R;
import com.chris.easymallproduct.dto.CategoryBrandRelationDto;
import com.chris.easymallproduct.entity.AttrEntity;
import com.chris.easymallproduct.entity.BrandEntity;
import com.chris.easymallproduct.entity.CategoryEntity;
import com.chris.easymallproduct.service.BrandService;
import com.chris.easymallproduct.service.CategoryService;
import com.chris.easymallproduct.vo.BrandsVo;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chris.common.utils.PageUtils;
import com.chris.common.utils.Query;

import com.chris.easymallproduct.dao.CategoryBrandRelationDao;
import com.chris.easymallproduct.entity.CategoryBrandRelationEntity;
import com.chris.easymallproduct.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void save(CategoryBrandRelationDto dto) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        BeanUtils.copyProperties(dto, categoryBrandRelationEntity);
        CategoryEntity categoryEntity = categoryService.getById(dto.getCatelogId());
        if (categoryEntity != null) {
            categoryBrandRelationEntity.setCatelogName(categoryEntity.getName());
        }
        BrandEntity brandEntity = brandService.getById(dto.getBrandId());
        if (brandEntity != null) {
            categoryBrandRelationEntity.setBrandName(brandEntity.getName());
        }
        this.save(categoryBrandRelationEntity);
    }

    @Override
    public List<CategoryBrandRelationEntity> catelogList(Long brandId) {
        return this.list(new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getBrandId, brandId));
    }

    @Override
    public List<BrandsVo> brandsList(Long catId) {
        return this.list(new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId))
                .stream()
                .map(categoryBrandRelationEntity -> {
                    BrandsVo vo = new BrandsVo();
                    vo.setBrandId(categoryBrandRelationEntity.getBrandId());
                    vo.setBrandName(categoryBrandRelationEntity.getBrandName());
                    return vo;
                }).collect(Collectors.toList());
    }


}